﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AMS.Core.Controllers;

namespace AMS.Core.WinForms
{
    public partial class RegisterDialog : Form
    {
        IRegisterController _controller;

        public bool EnableFirstName
        {
            get { return txtFirstName.Enabled; }
            set { txtFirstName.Enabled = value; }
        }

        public bool EnableLastName
        {
            get { return txtLastName.Enabled; }
            set { txtLastName.Enabled = value; }
        }

        public bool EnableEmail
        {
            get { return txtEmail.Enabled; }
            set { txtEmail.Enabled = value; }
        }

        public RegisterDialog(IRegisterController controller)
        {
            _controller = controller;
            _controller.RegistrationSucceeded += new EventHandler(HandleRegistrationSucceeded);
            _controller.RegistrationFailed += new EventHandler(HandleRegistrationFailed);

            InitializeComponent();
        }

        private void HandleBtnSubmit_Click(object sender, EventArgs e)
        {
            _controller.AttemptRegister(txtUsername.Text,
                txtPassword.Text,
                txtPassword2.Text,
                txtFirstName.Text,
                txtLastName.Text,
                txtEmail.Text);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RegisterDialog_Shown(object sender, EventArgs e)
        {
            txtUsername.Focus();
        }

        void HandleRegistrationSucceeded(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                MessageBox.Show("Registration Succeeded!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        void HandleRegistrationFailed(object sender, EventArgs e)
        {
            MessageBox.Show("Registration Failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        protected override void OnClosed(EventArgs e)
        {
            _controller.RegistrationSucceeded -= new EventHandler(HandleRegistrationSucceeded);
            base.OnClosed(e);
        }
    }
}
